/*
 * Created on 04/11/2006
 */
package com.minotauro.bean.property;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

import org.apache.commons.lang.SystemUtils;

import com.minotauro.cleda.util.FreeMarkerUtil;

/**
 * @author Demián Gutierrez
 */
public class CodeGenerator {

  private List<PropertyBean> propertyBeanList;

  private String className;

  private String out;
  private String pck;

  // --------------------------------------------------------------------------------

  public CodeGenerator(List<PropertyBean> propertyBeanList, String className, //
      String out, String pck) {

    this.propertyBeanList = propertyBeanList;

    this.className = className;

    this.out = out;
    this.pck = pck;
  }

  // --------------------------------------------------------------------------------

  public void run() throws Exception {

    ObjectBean objectBean = new ObjectBean();

    objectBean.setClsName("_Prop" + className);
    objectBean.setPckName(pck);
    objectBean.setPropertyBeanList(propertyBeanList);

    String aux = out.replace("/", SystemUtils.FILE_SEPARATOR);
    File folder = new File(aux);

    folder.mkdirs();

    FileWriter fileWriter = new FileWriter( //
        out + SystemUtils.FILE_SEPARATOR + objectBean.getClsName() + ".java");

    FreeMarkerUtil.getInstance().process("/templates/class-prop.ftl",
        objectBean, fileWriter);
    fileWriter.close();
  }
}
